#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;

#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;

// 自定义元素类型
typedef struct {
    int no;         // 学号
    char name[10];  // 姓名
    int score;      // 分数
} ElemType;

#define MAXSIZE 100
typedef struct {
    ElemType *elem;     // 存储空间的基地址
    int length;         // 当前长度
} SqList;

// 初始化学生课堂成绩管理程序（顺序表）
Status InitList(SqList &L) {
    L.elem = new ElemType[MAXSIZE];
    if (!L.elem) exit(OVERFLOW);
    L.length = 0;
    return OK;
}

// 销毁学生课堂成绩管理程序（顺序表）
void DestroyList(SqList &L) {
    if (L.elem) delete[] L.elem;
    L.elem = NULL;
    L.length = 0;
}

// 清空学生课堂成绩管理程序（顺序表）
Status ClearList(SqList &L) {
    L.length = 0;
    return OK;
}

// 判断学生课堂成绩管理程序（顺序表）是否为空
Status ListEmpty(SqList L) {
    return L.length == 0 ? OK : ERROR;
}

// 获取学生课堂成绩管理程序（顺序表）长度
int ListLength(SqList L) {
    return L.length;
}

// 按位序获取元素
Status GetElem(SqList L, int i, ElemType &e) {
    if (i < 1 || i > L.length) return ERROR;
    e = L.elem[i - 1];
    return OK;
}

// 按学号查找位序
int LocateElemByNo(SqList L, int no) {
    for (int i = 0; i < L.length; i++) {
        if (L.elem[i].no == no) return i + 1;
    }
    return 0;
}

// 为某学号同学增加指定分数
int LocateElemByNo_add(SqList L, int no,int score) {
    for (int i = 0; i < L.length; i++) {
        if (L.elem[i].no == no){
			L.elem[i].score+=score;
			return i + 1;
		} 
    }
    return 0;
}

// 为某学号同学减少指定分数
int LocateElemByNo_sub(SqList L, int no,int score) {
    for (int i = 0; i < L.length; i++) {
        if (L.elem[i].no == no){
			L.elem[i].score-=score;
			return i + 1;
		} 
    }
    return 0;
}

// 按姓名查找位序
int LocateElemByName(SqList L, const char *name) {
    for (int i = 0; i < L.length; i++) {
        if (strcmp(L.elem[i].name, name) == 0) return i + 1;
    }
    return 0;
}

// 按分数查找位序（返回第一个匹配的位置）
int LocateElemByScore(SqList L, int score) {
    for (int i = 0; i < L.length; i++) {
        if (L.elem[i].score == score) return i + 1;
    }
    return 0;
}

// 插入元素
Status ListInsert(SqList &L, int i, ElemType e) {
    if (i < 1 || i > L.length + 1) return ERROR;
    if (L.length == MAXSIZE) return ERROR;
    for (int j = L.length; j >= i; j--) {
        L.elem[j] = L.elem[j - 1];
    }
    L.elem[i - 1] = e;
    L.length++;
    return OK;
}

// 删除元素
Status ListDelete(SqList &L, int i, ElemType &e) {
    if (i < 1 || i > L.length) return ERROR;
    e = L.elem[i - 1];
    for (int j = i; j < L.length; j++) {
        L.elem[j - 1] = L.elem[j];
    }
    L.length--;
    return OK;
}

// 打印学生课堂成绩管理程序（顺序表）
void PrintList(SqList L) {
    if (ListEmpty(L) == OK) {
        cout << "学生课堂成绩管理程序（顺序表）为空" << endl;
        return;
    }
    cout << "序号\t学号\t姓名\t分数" << endl;
    cout << "--------------------------------" << endl;
    for (int i = 0; i < L.length; i++) {
        cout << i + 1 << "\t" << L.elem[i].no << "\t" 
             << L.elem[i].name << "\t" << L.elem[i].score << endl;
    }
}

// 从文件导入数据创建学生课堂成绩管理程序（顺序表）
Status ImportListFromFile(SqList &L, const string &filename) {
    ifstream file(filename);
    if (!file.is_open()) {
        cout << "无法打开文件: " << filename << endl;
        return ERROR;
    }
    
    // 清空当前学生课堂成绩管理程序（顺序表）
    ClearList(L);
    
    ElemType value;
    while (file >> value.no >> value.name >> value.score && L.length < MAXSIZE) {
        L.elem[L.length] = value;
        L.length++;
    }
    
    file.close();
    cout << "成功从文件导入 " << L.length << " 个元素" << endl;
    return OK;
}

// 保存学生课堂成绩管理程序（顺序表）到文件
Status ExportListToFile(SqList L, const string &filename) {
    ofstream file(filename);
    if (!file.is_open()) {
        cout << "无法创建文件: " << filename << endl;
        return ERROR;
    }
    
    for (int i = 0; i < L.length; i++) {
        file << L.elem[i].no << " " << L.elem[i].name << " " << L.elem[i].score << endl;
    }
    
    file.close();
    cout << "成功导出 " << L.length << " 个元素到文件: " << filename << endl;
    return OK;
}

// 输入一个ElemType元素
void InputElem(ElemType &e) {
    cout << "请输入学号: ";
    cin >> e.no;
    cout << "请输入姓名: ";
    cin >> e.name;
    cout << "请输入分数: ";
    cin >> e.score;
}

// 显示菜单
void ShowMenu() {
    cout << "\n======= 学生课堂成绩管理程序（顺序表）操作菜单 =======" << endl;
    cout << "1. 初始化学生课堂成绩管理程序（顺序表）" << endl;
    cout << "2. 销毁学生课堂成绩管理程序（顺序表）" << endl;
    cout << "3. 清空学生课堂成绩管理程序（顺序表）" << endl;
    cout << "4. 判断学生课堂成绩管理程序（顺序表）是否为空" << endl;
    cout << "5. 获取学生课堂成绩管理程序（顺序表）长度" << endl;
    cout << "61. 按位序获取学生信息" << endl;
    cout << "62. 按学号查找返回位序" << endl;
    cout << "63. 按姓名查找返回位序" << endl;
    cout << "64. 按分数查找返回位序" << endl;
    cout << "71. 为某学号同学增加指定分数" << endl;
    cout << "72. 为某学号同学减少指定分数" << endl;
    cout << "10. 插入新学生信息" << endl;
    cout << "11. 删除已有学生信息" << endl;
    cout << "12. 打印学生课堂成绩管理程序（顺序表）" << endl;
    cout << "13. 从文件导入数据" << endl;
    cout << "14. 保存数据到文件" << endl;
    cout << "0. 退出程序" << endl;
    cout << "===============================" << endl;
    cout << "请选择操作: ";
}

int main() {
    SqList L;
    int choice, position;
    ElemType value;
    Status result;
    string filename;
    char name[10];
    
    // 初始时学生课堂成绩管理程序（顺序表）未初始化
    L.elem = NULL;
    L.length = 0;
    
    do {
        ShowMenu();
        cin >> choice;
        
        switch (choice) {
            case 1:
                if (L.elem != NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）已存在，请先销毁!" << endl;
                } else {
                    result = InitList(L);
                    if (result == OK) 
                        cout << "学生课堂成绩管理程序（顺序表）初始化成功!" << endl;
                    else 
                        cout << "学生课堂成绩管理程序（顺序表）初始化失败!" << endl;
                }
                break;
                
            case 2:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    DestroyList(L);
                    cout << "学生课堂成绩管理程序（顺序表）已销毁!" << endl;
                }
                break;
                
            case 3:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    ClearList(L);
                    cout << "学生课堂成绩管理程序（顺序表）已清空!" << endl;
                }
                break;
                
            case 4:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    result = ListEmpty(L);
                    if (result == OK) 
                        cout << "学生课堂成绩管理程序（顺序表）为空!" << endl;
                    else 
                        cout << "学生课堂成绩管理程序（顺序表）不为空!" << endl;
                }
                break;
                
            case 5:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "学生课堂成绩管理程序（顺序表）长度为: " << ListLength(L) << endl;
                }
                break;
                
            case 61:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "请输入要获取的元素位置: ";
                    cin >> position;
                    result = GetElem(L, position, value);
                    if (result == OK) {
                        cout << "第" << position << "个元素是: " << endl;
                        cout << "学号: " << value.no << ", 姓名: " << value.name << ", 分数: " << value.score << endl;
                    } else {
                        cout << "位置不合法!" << endl;
                    }
                }
                break;
                
            case 62:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "请输入要查找的学号: ";
                    cin >> value.no;
                    position = LocateElemByNo(L, value.no);
                    if (position != 0) 
                        cout << "学号为" << value.no << "的学生位于第" << position << "个位置" << endl;
                    else 
                        cout << "未找到学号为" << value.no << "的学生" << endl;
                }
                break;
                
            case 63:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "请输入要查找的姓名: ";
                    cin >> name;
                    position = LocateElemByName(L, name);
                    if (position != 0) 
                        cout << "姓名为" << name << "的学生位于第" << position << "个位置" << endl;
                    else 
                        cout << "未找到姓名为" << name << "的学生" << endl;
                }
                break;
                
            case 64:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "请输入要查找的分数: ";
                    cin >> value.score;
                    position = LocateElemByScore(L, value.score);
                    if (position != 0) 
                        cout << "分数为" << value.score << "的学生位于第" << position << "个位置" << endl;
                    else 
                        cout << "未找到分数为" << value.score << "的学生" << endl;
                }
                break;

            case 71:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "请输入要增加分数的学生学号和分数: ";
                    cin >> value.no >> value.score;
                    position = LocateElemByNo_add(L, value.no, value.score);
                    if (position != 0) 
                        cout << "学号为" << value.no << "的学生已经增加" << value.score << "分数" << endl;
                    else 
                        cout << "未找到学号为" << value.no << "的学生" << endl;
                }
                break;

            case 72:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "请输入要减少分数的学生学号和分数: ";
                    cin >> value.no >> value.score;
                    position = LocateElemByNo_sub(L, value.no, value.score);
                    if (position != 0) 
                        cout << "学号为" << value.no << "的学生已经减少" << value.score << "分数" << endl;
                    else 
                        cout << "未找到学号为" << value.no << "的学生" << endl;
                }
                break;
				                
            case 10:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "请输入要插入的位置: ";
                    cin >> position;
                    InputElem(value);
                    result = ListInsert(L, position, value);
                    if (result == OK) 
                        cout << "插入成功!" << endl;
                    else 
                        cout << "插入失败!位置不合法或学生课堂成绩管理程序（顺序表）已满" << endl;
                }
                break;
                
            case 11:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    cout << "请输入要删除的位置: ";
                    cin >> position;
                    result = ListDelete(L, position, value);
                    if (result == OK) {
                        cout << "删除成功!被删除的学生信息是: " << endl;
                        cout << "学号: " << value.no << ", 姓名: " << value.name << ", 分数: " << value.score << endl;
                    } else {
                        cout << "删除失败!位置不合法" << endl;
                    }
                }
                break;
                
            case 12:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else {
                    PrintList(L);
                }
                break;
                
            case 13:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化，请先初始化!" << endl;
                } else {
                    cout << "请输入要导入的文件名: ";
                    cin >> filename;
                    result = ImportListFromFile(L, filename);
                    if (result == ERROR) {
                        cout << "导入文件失败!" << endl;
                    }
                }
                break;
                
            case 14:
                if (L.elem == NULL) {
                    cout << "学生课堂成绩管理程序（顺序表）未初始化!" << endl;
                } else if (ListEmpty(L) == OK) {
                    cout << "学生课堂成绩管理程序（顺序表）为空，无法导出!" << endl;
                } else {
                    cout << "请输入要保存的文件名: ";
                    cin >> filename;
                    result = ExportListToFile(L, filename);
                    if (result == ERROR) {
                        cout << "导出文件失败!" << endl;
                    }
                }
                break;
                
            case 0:
                if (L.elem != NULL) {
                    DestroyList(L);
                }
                cout << "程序已退出!" << endl;
                break;
                
            default:
                cout << "无效的选择，请重新输入!" << endl;
        }
    } while (choice != 0);
    
    return 0;
}
